package b1.scenes.fight
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	
	import com.greensock.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class MagicPane extends Sprite
	{
		static private var instance:MagicPane;
		public const iconNumInRow:int = 4;
		public var iconCount:int = 0; 
		private const iconWidth:Number = 60;
		private var container:Sprite;
		
		public function MagicPane()
		{
			super();
		}
		
		static public function getInstance():MagicPane
		{
			if (!instance)
				instance = new MagicPane();
			return instance;
		}
		
		public function refresh(animalId:int, forbidTime:int = -1):void
		{
			if (container != null && container.parent != null)
			{
				container.parent.removeChild(container);
				container = null;
			}
			container = new Sprite();
			addChild(container);
			iconCount = 0;
			//加载物理攻击图标
			var BoneAttackIcon:Class = ResourceHelper.getInstance().getClassByName("BoneAttackIcon");
			var boneAttackIcon:BasicButton = new BasicButton(BoneAttackIcon);
			TweenLite.from(boneAttackIcon, 1, {delay: iconCount / 5, x:100, alpha:0});
			container.addChild(boneAttackIcon);
			boneAttackIcon.addEventListener(MouseEvent.CLICK, onBoneAttackIconMouseClick);
			iconCount++;
			if (forbidTime >= 0) return;
			//魔法图标
			var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[animalId];
			var magicCooldown:int;
			for (var position:String in animalInfo.equips)
			{
				var equipInfo:EquipInfo = animalInfo.equips[position];
				var equipConfig:EquipConfig = Config.objectConfigs[equipInfo.objectIndex];
				var magicConfig:MagicConfig = Config.magicConfigs[equipConfig.magicIndex];
				var valid:Boolean = (magicConfig.vitalityCost[equipConfig.color] <= animalInfo.vitalityPoint);
				if (animalInfo.cooldown[equipConfig.magicIndex] != null)
					magicCooldown = animalInfo.cooldown[equipConfig.magicIndex];
				else
					magicCooldown = magicConfig.cooldown[equipConfig.color];
				if (SceneContainer.getInstance().step > 0)
				{
					magicCooldown = 0;
					valid = true;
				}
				var magicIcon:MagicIcon = new MagicIcon(equipConfig.magicIndex, equipConfig.color, magicCooldown, valid);
				var posX:int = (iconCount % iconNumInRow) * (iconWidth + 10); 
				magicIcon.x = posX;
				magicIcon.y = Math.floor(iconCount / iconNumInRow) * (iconWidth + 10);
				TweenLite.from(magicIcon, 1, {delay: iconCount / 5, x:posX + 100, alpha:0});
				container.addChild(magicIcon);
				iconCount++;
			}
			//加载装备整体魔法
			var wholeEquipConfig:EquipConfig = animalInfo.getWholeEquipConfig();
			if (wholeEquipConfig != null)
			{
				magicConfig = Config.magicConfigs[wholeEquipConfig.wholeMagicIndex];
				valid = (magicConfig.vitalityCost[wholeEquipConfig.color] <= animalInfo.vitalityPoint);
				if (animalInfo.cooldown[wholeEquipConfig.wholeMagicIndex] != null)
					magicCooldown = animalInfo.cooldown[wholeEquipConfig.wholeMagicIndex];
				else
					magicCooldown = magicConfig.cooldown[wholeEquipConfig.color];
				if (SceneContainer.getInstance().step > 0)
				{
					magicCooldown = 0;
					valid = true;
				}
				magicIcon = new MagicIcon(wholeEquipConfig.wholeMagicIndex, wholeEquipConfig.color, magicCooldown, valid);
				posX = (iconCount % iconNumInRow) * (iconWidth + 10);
				magicIcon.x = posX;
				magicIcon.y = Math.floor(iconCount / iconNumInRow) * (iconWidth + 10);
				TweenLite.from(magicIcon, 1, {delay: iconCount / 5, x:posX + 100, alpha:0});
				container.addChild(magicIcon);
				iconCount++;
			}
		}
		
		private function onBoneAttackIconMouseClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			if (SceneContainer.getInstance().step == 19)
			{
				SceneContainer.getInstance().clearLastStep();
			}
			fight.leftAnimal.throwPreparedBone();
			fight.resumeGame();
			close();
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
	}
}